#coding=utf-8
# Create your views here.
from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib import auth
from member.functions import *
from library.functions import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User

@login_required
def index(request):
    message = 'hi, im user index!'
    return render_to_response('member/index.html', locals())
    
def login(request):
    if request.META['REQUEST_METHOD'] != 'POST':
        next     = request.REQUEST.get('next', '/')
        return render_to_response('member/login.html', locals())
    else:
        account  = request.POST.get('account', '')
        password = request.POST.get('password', '')
        next     = request.REQUEST.get('next', '/')

        # 验证登陆是否正确
        login_user = authenticate(account = account, password = password)
        
        if login_user is not None and login_user.is_active:
            auth.login(request, login_user)
            
            # 回到上一页
            #if next != '':
            #    return HttpResponseRedirect(next)
            #return HttpResponseRedirect('/')
            return showSystemMessage("登陆成功,现在为您跳转...", next)
        else:
            return showSystemMessage("登陆失败, : (", next)
        


def logout(request):
    next     = request.REQUEST.get('next', '/')
    auth.logout(request)
    
    if request.is_ajax():
        return showJson(True)
    else:
        return showSystemMessage("登出成功,现在为您跳转...", next)
    
def register(request):
    if request.META['REQUEST_METHOD'] != 'POST':
        next     = request.REQUEST.get('next', '/')
        return render_to_response('member/register.html', locals())
    else:
        username    = request.POST.get('username', '').strip(' \'\\\"')
        email       = request.POST.get('email', '').strip(' \'\\\"')
        nickname    = request.POST.get('nickname', '').strip(' \'\\\"')
        password    = request.POST.get('password', '').strip(' \'\\\"')
        password2   = request.POST.get('password2', '').strip(' \'\\\"')
        gender      = request.POST.get('gender', 'none').strip(' \'\\\"')
        description = request.POST.get('description', '')
        next        = request.REQUEST.get('next', '/')
        
        if password != password2 or password == '':
            return showSystemMessage("密码错误", request.path)
        
        if username == '':
            return showSystemMessage("用户名不能为空", request.path)
            
        try:
            member = add_member(username = username, email = email, nickname = nickname, password = password, gender = gender, description = description)
            if not member:
                raise UnknowError, '注册失败，请稍候再试...'
            else:
                #自动登陆
                member = authenticate(account = member.username, password = password)
                auth.login(request, member)
        except Exception, e:
            return showSystemMessage(e.message, request.path)
        
        return showSystemMessage("注册成功,现在为您跳转...", next)

@login_required        
def follow(request):
    next     = request.REQUEST.get('next', '/')
    if request.META['REQUEST_METHOD'] != 'POST':
        return showSystemMessage('不支持此操作', next)
    else:
        user_id = request.REQUEST.get('user_id', 0)
        
        if user_id == 0:
            if request.is_ajax():
                return showJson({result : False, reason : '没有此用户'})
            else:
                return showSystemMessage('没有此用户', next)
        else:
            try:
                followee = get_user(id = user_id)
                follow_user(request.user, followee)
            except User.DoesNotExist, e:
                if request.is_ajax():
                    return showJson({result : False, reason : '没有此用户'})
                else:
                    return showSystemMessage('没有此用户', next)
            except BanedUser, e:
                if request.is_ajax():
                    return showJson({result : False, reason : '用户禁止被follow'})
                else:
                    return showSystemMessage('用户禁止被follow', next)
        if request.is_ajax():
            return showJson({result : True, reason : 'follow已成功'})       
        else:
            return showSystemMessage('follow已成功，现在为您跳转...', next)
